package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.jaredrummler.materialspinner.MaterialSpinner
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.PsDaolu
import com.wlzg.collectiontool.model.bean.PsGongcheng
import com.wlzg.collectiontool.model.bean.PsXiangmu
import com.wlzg.collectiontool.util.DateUtil
import kotlinx.android.synthetic.main.new_gongcheng.view.*
import kotlinx.android.synthetic.main.new_gongcheng.view.diss
import kotlinx.android.synthetic.main.new_gongcheng.view.quxiao
import kotlinx.android.synthetic.main.new_gongcheng.view.shanchu
import kotlinx.android.synthetic.main.new_gongcheng.view.status
import kotlinx.android.synthetic.main.new_gongcheng.view.zx
import kotlinx.android.synthetic.main.new_gongcheng.view.zy
import kotlinx.android.synthetic.main.new_pro.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class NewGcDia(var mContext: Context, var mFragment: ManageFragment, var gc: PsGongcheng?,var dia:GclistDia?=null) :
    PositionPopupView(mContext),
    View.OnClickListener, MaterialSpinner.OnItemSelectedListener<Any> {
    var proList: ArrayList<PsXiangmu?>? = ArrayList()
    var dlList: ArrayList<PsDaolu?>? = ArrayList()
    var ssxmid: Long? = 0L
    var ssdlid: Long? = 0L
    private var statusCode = 0;
    override fun getImplLayoutId(): Int {
        return R.layout.new_gongcheng
    }

    override fun onCreate() {
        super.onCreate()
        diss.setOnClickListener(this)
        shanchu.setOnClickListener(this)
        quxiao.setOnClickListener(this)
        baocun.setOnClickListener(this)
        zx.setText(mFragment.gcPoint?.x.toString())
        zy.setText(mFragment.gcPoint?.y.toString())
        status.setItems("进行中", "完成")
        status.setOnItemSelectedListener { view, position, id, item ->
            status.setText(item as? String)
            statusCode = position
        }
        if (gc != null) {
            initSpinner()

        }
    }

    override fun onShow() {
        super.onShow()
        if (gc == null) {
            initSpinner()
        }
    }

    private fun initSpinner() {
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                proList = AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                dlList = AppDatabase.getInstance().userDao().getdlList() as ArrayList<PsDaolu?>
            }
            ssdaolu.setItems(dlList as ArrayList<PsDaolu>)
            ssxiangmu.setItems(proList as ArrayList<PsXiangmu>)
            ssxiangmu.setOnItemSelectedListener(this@NewGcDia)
            ssdaolu.setOnItemSelectedListener(this@NewGcDia)
            if (proList != null) {
                if (proList!!.size > 0) {
                    ssxmid = proList!!.get(0)?.xiangmuId
                }
            }
            gc?.let {
                initData(it)
            }
        }
    }

    private fun getText(): PsGongcheng {
        val psGc = if (gc != null) {
            gc as PsGongcheng
        } else {
            PsGongcheng()
        }
        psGc.gcName = gcName.text.toString()
        psGc.ssdlId = ssdlid!!
        psGc.ssxmId = ssxmid!!
        psGc.gcStatus = statusCode.toString()
        psGc.teamleader = zuzhang.text.toString()
        psGc.x1Coor = zx.text.toString().toDouble()
        psGc.y1Coor = zy.text.toString().toDouble()
        psGc.updateTime = DateUtil.getCurData()
        return psGc
    }

    private fun initData(psGc: PsGongcheng) {
        gcName.setText(psGc.gcName)
        mFragment.lifecycleScope.launch(context = Dispatchers.IO) {
            val xm = AppDatabase.getInstance().userDao().getXmListbyId(psGc.ssxmId)
            Log.d("sjskks", "k" + psGc.ssdlId)
            val index = proList?.indexOf(xm)
            val dl = AppDatabase.getInstance().userDao().getDlbyId(psGc.ssdlId)
            val dlindex = dlList?.indexOf(dl)
            withContext(Dispatchers.Main) {
                if (index != null && index > -1) {
                    ssxiangmu.selectedIndex = index
                }

                if (dlindex != null && dlindex > -1) {
                    ssdaolu.selectedIndex = dlindex
                }

            }

        }
        zuzhang.setText(psGc.teamleader)
        try {
            status.selectedIndex = psGc.gcStatus?.toInt()!!
        } catch (e: Exception) {

        }
        zx.setText(psGc.x1Coor.toString())
        zy.setText(psGc.y1Coor.toString())
        ssxmid = psGc.ssxmId


    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.diss -> {
                mFragment.ondelete(2, null)
                dismiss()
            }
            R.id.shanchu -> {
                mFragment.ondelete(2, gc)
                dismiss()
            }
            R.id.quxiao -> {
                mFragment.ondelete(2, null)
                dismiss()
            }
            R.id.baocun -> {
                dismiss()
                mFragment.lifecycleScope.launch(context = Dispatchers.IO) {
                    if (gc == null) {
                        val ids = AppDatabase.getInstance().userDao().insertPsGongcheng(getText())
                        Log.d("THSS", "ID:" + ssxmid)
                    } else {
                        AppDatabase.getInstance().userDao().updateGc(getText())
                    }
                    withContext(Dispatchers.Main){
                        dia?.onUpdate()}

                }
            }
        }
    }

    override fun onItemSelected(view: MaterialSpinner?, position: Int, id: Long, item: Any?) {
        when (view?.id) {
            R.id.ssxiangmu -> {
                item as PsXiangmu
                ssxmid = item.xiangmuId
            }
            R.id.ssdaolu -> {
                item as PsDaolu
                ssdlid = item.daoluId

            }
        }
    }
}